<?php

namespace JLPay\Models\Ledger\BalanceSplit;

use JLPay\Models\BaseResponse;

/**
 * 余额分账查询响应
 */
class BalanceSplitQueryResponse extends BaseResponse
{
    protected ?string $orgCode = null;
    protected ?string $outOrderNo = null;
    protected ?string $orderNo = null;
    protected ?string $totalAmount = null;
    protected ?string $state = null;
    protected ?array $receivers = null;

    /**
     * 构造函数
     *
     * @param array|null $responseArray
     */
    public function __construct(?array $responseArray = null)
    {
        parent::__construct($responseArray);

        if ($responseArray) {
            $this->orgCode = $responseArray['org_code'] ?? null;
            $this->outOrderNo = $responseArray['out_order_no'] ?? null;
            $this->orderNo = $responseArray['order_no'] ?? null;
            $this->totalAmount = $responseArray['total_amount'] ?? null;
            $this->state = $responseArray['state'] ?? null;
            
            // 将 receivers 数组转换为 Receiver 对象数组
            $receiversData = $responseArray['receivers'] ?? [];
            $this->receivers = [];
            foreach ($receiversData as $receiverArray) {
                $receiver = new Receiver($receiverArray);
                $this->receivers[] = $receiver;
            }
        }
    }

    /**
     * 获取机构代码
     *
     * @return string|null
     */
    public function getOrgCode(): ?string
    {
        return $this->orgCode;
    }

    /**
     * 获取外部订单号
     *
     * @return string|null
     */
    public function getOutOrderNo(): ?string
    {
        return $this->outOrderNo;
    }

    /**
     * 获取订单号
     *
     * @return string|null
     */
    public function getOrderNo(): ?string
    {
        return $this->orderNo;
    }

    /**
     * 获取分账总金额
     *
     * @return string|null
     */
    public function getTotalAmount(): ?string
    {
        return $this->totalAmount;
    }

    /**
     * 获取状态
     *
     * @return string|null
     */
    public function getState(): ?string
    {
        return $this->state;
    }

    /**
     * 获取分账接收方列表
     *
     * @return array|null
     */
    public function getReceivers(): ?array
    {
        return $this->receivers;
    }

    /**
     * 转换为数组
     *
     * @return array
     */
    public function toArray(): array
    {
        $data = parent::toArray();
        
        // 正确处理receivers字段
        if ($this->receivers) {
            $data['receivers'] = [];
            foreach ($this->receivers as $receiver) {
                $data['receivers'][] = $receiver->toArray();
            }
        }
        
        return $data;
    }
} 